#!/usr/bin/env bash

function getShellColor(){
    local color="0";
    case $1 in
        red) color="1";
        ;;
        green) color="2";
        ;;
        yellow) color="3";
        ;;
        blue) color="4";
        ;;
        purple) color="5";
        ;;
        blues) color="6";
        ;;
        *) color="0";
        ;;
    esac
    return $color;
}
function getShellStyle(){
    local style="3";
    case $1 in
        _) style="2";
        ;;
        b) style="4";
        ;;
        *) style="3";
        ;;
    esac
    return $style;
}
function getStyle(){
    local color="0";
    local style="3";
    local styleColor="0";
    getShellColor "$1";
    color=$?;
    getShellColor "$2";
    styleColor=$?;
    getShellStyle "$3";
    style=$?;
    echo -n -e "\033[${style}${styleColor};3${color}""m";
}
function print(){
    if [ "" != "$2" ]; then
        getStyle "$2" "$3" "$4";
    fi
    echo -n -e "$1""\033[0m";
}
function println(){
    if [ "" != "$2" ]; then
        getStyle "$2" "$3" "$4";
    fi
    echo -e "$1""\033[0m";
}

# config Alias
alias rm='rm';
alias cp='cp';
alias mv='mv';
# config Path
commandSelf="$0";
commandDir=$(dirname "${commandSelf}");
# shellcheck disable=SC2164
commandPath=$(cd "${commandDir}"; pwd;);
# config variable
protoPath=${commandPath};
# shellcheck disable=SC2164
outNamespacePath=$(cd "${commandPath}/../../../"; pwd;);
outGPBMetadataName="${outNamespacePath}/GPBMetadata";
protoFilePath="${outNamespacePath}/Srv/Apps/Proto";
# proto file List
protoPathList=(
    "${protoPath}/"
);

println "delete Old Proto file" blue;
rm -rf "${protoFilePath}";
mkdir -p "${protoFilePath}/"{BO,DBO,DTO,VO,Enum};

println "protoc path:" blue;
# shellcheck disable=SC2068
for path in ${protoPathList[@]}; do
    if [ ! -d "$path" ] ; then continue; fi
    find "$path" -type f -name "*.proto" -exec protoc --proto_path /program/protobuf/src/ --proto_path="${protoPath}" --php_out="${outNamespacePath}" {} \;
    # shellcheck disable=SC2181
    if [ $? -eq 0 ] ; then
        println "Success ${path}" green;
    else
        println "Failed  ${path}" red;
    fi
done

# GPBMetadata
println "replace GPBMetadata" blue;
mv -f "${outGPBMetadataName}" "${protoFilePath}";
find "${protoFilePath}/GPBMetadata/" -type f -name '*.php' -exec sed -i -e 's/namespace\s\{1,\}GPBMetadata/namespace Srv\\Apps\\Proto\\GPBMetadata/g' {} \;
find "${protoFilePath}/" -type f -name '*.php' -exec sed -i -e 's/\s\\GPBMetadata\\\(BO\|DBO\|DTO\|VO\|Enum\\\)/ \\Srv\\Apps\\Proto\\GPBMetadata\\\1/g' {} \;

# class const
println "replace class const" blue;
# shellcheck disable=SC2016
find "${protoFilePath}/" -type f -name '*.php' -exec sed -i -r 's#(protected|private|public)\s+\$(\w+)(\s+=\s+\S+)?;#'"\0\n    public const _\2 = '\2';"'#g' {} \;

# extends JsonSerializable
println "replace extends OAbstract" blue;
find "${protoFilePath}/DBO/" -type f -name '*.php' -exec sed -i -e 's/\sextends\s\{1,\}\\Google\\Protobuf\\Internal\\Message/ extends \\Srv\\Libs\\Frame\\DBOAbstract/g' {} \;
find "${protoFilePath}/BO/" -type f -name '*.php' -exec sed -i -e 's/\sextends\s\{1,\}\\Google\\Protobuf\\Internal\\Message/ extends \\Srv\\Libs\\Frame\\BOAbstract/g' {} \;
find "${protoFilePath}/VO/" -type f -name '*.php' -exec sed -i -e 's/\sextends\s\{1,\}\\Google\\Protobuf\\Internal\\Message/ extends \\Srv\\Libs\\Frame\\VOAbstract/g' {} \;
find "${protoFilePath}/DTO/" -type f -name '*.php' -exec sed -i -e 's/\sextends\s\{1,\}\\Google\\Protobuf\\Internal\\Message/ extends \\Srv\\Libs\\Frame\\DTOAbstract/g' {} \;

# replace GPBUtil
println "replace GPBUtilAbstract" blue;
find "${protoFilePath}/DBO/" -type f -name '*.php' -exec sed -i -r 's#(use\s+Google\\Protobuf\\Internal\\GPBUtil;)#\1\nuse Srv\\Libs\\Frame\\GPBUtilAbstract;#g' {} \;
find "${protoFilePath}/DBO/" -type f -name '*.php' -exec sed -i -r 's#(\s+)GPBUtil::(check[^\(]+\()#\1GPBUtilAbstract::GPB\2__METHOD__, #g' {} \;
find "${protoFilePath}/DBO/" -type f -name '*.php' -exec sed -i -r 's#(\s+)return\s+([^;]+);#\1return GPBUtilAbstract::GPBgetVal(__METHOD__, \2);#g' {} \;

# enum
# shellcheck disable=SC2016
find "${protoFilePath}/" -type f -name '*Enum.php' -exec sed -i -r 's#private\s+static\s+\$valueToName#'"protected static array \$valueToName"'#g' {} \;
find "${protoFilePath}/" -type f -name '*Enum.php' -exec sed -i -r 's#\sconst\s+(\w+)(\s+=\s+\S+)?;#'" public\0\n    public const _\1 = '\1';"'#g' {} \;
find "${protoFilePath}/" -type f -name '*Enum.php' -exec sed -i -r 's#^class\s+(\w+)Enum$#'"\0 extends \\\Srv\\\Libs\\\Frame\\\EnumAbstract"'#g' {} \;

# replace @ char
find "${protoFilePath}/" -type f -name '*.php' -exec sed -i -r 's#&\#64;\[#@\[#g' {} \;

println "finish." blue;
